package com.etop.learnapp.fragment;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.etop.learnapp.R;
import com.etop.learnapp.activity.PicActivity;
import com.etop.learnapp.me.drakeet.materialdialog.MaterialDialog;
import com.etop.learnapp.util.BitmapCreate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2015/10/26 0026.
 */
public class HomeworkFragment extends Fragment {
    LinearLayout mLienarLayout;
    List<View> list ;
    private String ImageUrihome;
    private String HomeUri;
    private String AnsUri;
    private String ImageUrians;
    ImageView iv;
    Button bn;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container , Bundle saveInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_homework  ,container,false);
        mLienarLayout = (LinearLayout) rootView.findViewById(R.id.ll_homework);
        list = new ArrayList<>();
        int chapterSelect = getActivity().getIntent().getIntExtra("chapterSelect", -1);
        int sectionSelect = getActivity().getIntent().getIntExtra("sectionSelect", -1);
        int keySelect = getActivity().getIntent().getIntExtra("keySelect", -1);
        AssetManager am = getActivity().getAssets();

        ImageUrihome = "picture/"+chapterSelect + "_" + sectionSelect + "_" + keySelect+"/homework";
        ImageUrians = "picture/"+chapterSelect + "_" + sectionSelect + "_" + keySelect+"/answer";
        int num=0;
        try {
            num = am.list(ImageUrihome).length;
            System.out.println(ImageUrihome);
            System.out.println(num);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i=1;i<=num;i++){
            iv = new ImageView(getContext());
            HomeUri = "assets://"+ImageUrihome+"/"+i+".jpg";
            AnsUri = "assets://"+ImageUrians+"/"+i+".jpg";
            iv = new ImageView(getContext());
            iv.setScaleType(ImageView.ScaleType.FIT_START);
            iv.setAdjustViewBounds(true);
            new BitmapCreate(HomeUri,getContext(),iv);
            iv.setOnClickListener(new View.OnClickListener() {
                                      String UR = HomeUri;

                                      @Override
                                      public void onClick(View v) {
                                          Intent intent = new Intent(getActivity(), PicActivity.class);
                                          intent.putExtra("Resources", UR);
                                          startActivity(intent);
                                      }
                                  }
            );
            list.add(iv);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(5,10,5,0);
            iv.setLayoutParams(lp);

            mLienarLayout.addView(iv);

            bn = new Button(getContext());
            bn.setText("查看答案");
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            bn.setLayoutParams(params);
            bn.setOnClickListener(new View.OnClickListener() {
                                      MaterialDialog mMaterialDialog;
                                      String UR=AnsUri;
                                      @Override
                                      public void onClick(View v) {
                                          mMaterialDialog = new MaterialDialog(getContext()).setTitle("答案");
                                          ImageView ig = new ImageView(getContext());
                                          new BitmapCreate(UR, getContext(), ig);
                                          mMaterialDialog.setView(ig);
                                          mMaterialDialog.setPositiveButton("OK", new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {
                                                  mMaterialDialog.dismiss();
                                              }
                                          });
                                          mMaterialDialog.show();
                                      }
                                  }
        );
            mLienarLayout.addView(bn);
            list.add(bn);
        }
        return rootView;
    }
}
